package com.macasa.panel.web.managedbean;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import com.macasa.panel.web.auth.api.AuthenticationService;

@ManagedBean(name = "loginMB")
@RequestScoped
public class LoginBean implements Serializable {

	private static final long		serialVersionUID	= 6684969594517233446L;

	private String					login;
	private String					password;

	@ManagedProperty(value = "#{authenticationService}")
	private AuthenticationService	authenticationService;						// injected Spring defined service for bikes

	public String login() {

		final boolean success = this.authenticationService.login(this.login, this.password);

		if (success) {
			return "faces/bikesShop.xhtml";
		} else {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Wrong login or password"));
			return "faces/login.xhtml";
		}
	}

	public String getLogin() {
		return this.login;
	}

	public void setLogin(final String login) {
		this.login = login;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(final String password) {
		this.password = password;
	}

	public void setAuthenticationService(final AuthenticationService authenticationService) {
		this.authenticationService = authenticationService;
	}
}
